﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SendEmail
{
    public class Win32Const
    {
        public static int WM_SETTEXT = 0x000C;
        public static int WM_COMMAND = 0x0111;
    }
    public class Win32API
    {

        // [DllImport("user32.dll", EntryPoint = "FindWindow")]
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        ///<summary>
        /// 设置指定窗口为当前活动窗口
        ///</summary>
        ///<param name="hWnd">窗口句柄</param>
        [DllImport("User32.DLL")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        ///<summary>
        /// 向指定窗口发送字符串
        ///</summary>
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        [DllImport("shell32.dll")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam);
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
        static IntPtr Game;



        const int BM_CLICK = 0xF5;

        public static IntPtr FindWind(string sTitle)
        {
            IntPtr hwnd = FindWindow(null, sTitle);
            return hwnd;
        }

        public class WindowInfo
        {

            public IntPtr hWnd;
            public string szClassName;
            public string szWindowName;


        }

        //寻找系统的全部窗口
        public static WindowInfo[] GetAllDesktopWindows()
        {
            List<WindowInfo> wndList = new List<WindowInfo>();
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
                //get hwnd
                wnd.hWnd = hWnd;
                //get window name
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();
                //get window class
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();
                //Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);
                //add it into list
                if (wnd.szClassName == "TXGuiFoundation" && wnd.szWindowName == "查找")
                {
                    wndList.Add(wnd);
                }

                return true;
            }, 0);
            return wndList.ToArray();
        }

        public static bool FindSearchWind()
        {

            IntPtr maindHwnd = FindWindow("TXGuiFoundation", "");

            if (maindHwnd != IntPtr.Zero)
            {

                IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "登录");
                if (childHwnd != IntPtr.Zero)
                {
                    SendMessage(childHwnd, BM_CLICK, 0, 0);
                }
                else
                {
                    Console.WriteLine("没有找到子窗口");
                }

            }

            return (maindHwnd != IntPtr.Zero);

        }




    }
}
